package org.jks.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jks.constant.Constants;
import org.jks.excel.DeviceInOutUtil;
import org.jks.model.Device;
import org.jks.model.DeviceInOut;
import org.jks.model.DeviceStatus;
import org.jks.model.User;
import org.jks.report.model.ReportDeviceInOut;
import org.jks.request.model.Ids;
import org.jks.request.model.UIDeviceInOut;
import org.jks.request.model.UISearchDIO;
import org.jks.response.model.ActionResult;
import org.jks.response.model.UITable;
import org.jks.service.DeviceInOutService;
import org.jks.service.DeviceService;
import org.jks.service.WorkService;
import org.jks.util.DateUtil;
import org.jks.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/device_in_out")
public class DeviceInOutController {
	@Autowired
	DeviceService deviceService;
	@Autowired
	DeviceInOutService deviceInOutService;
	@Autowired
	WorkService workService;
	
	
	@RequestMapping("/add")
	public void add(UIDeviceInOut uiDeviceInOut, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) {
		ActionResult ar = new ActionResult();
		User user = (User) session.getAttribute(Constants.SESSION_ACCOUNT);
		try {
			DeviceInOut deviceInOut = new DeviceInOut();
			deviceInOut.setType(uiDeviceInOut.getType());
			Device device = deviceService.findById(uiDeviceInOut.getDeviceId());
			deviceInOut.setNo(device.getNo());
			deviceInOut.setTdCode(device.getTdCode());
			deviceInOut.setBrand(device.getBrand());
			deviceInOut.setDeviceType(device.getType().getName());
			deviceInOut.setManufacturers(device.getManufacturers());
			deviceInOut.setModel(device.getModel());
			deviceInOut.setOperator(user);
			deviceInOut.setTime(DateUtil.toDate(uiDeviceInOut.getTime(), "yyyy-MM-dd HH:mm:ss"));
			deviceInOutService.save(deviceInOut);
			ar.setSuccess(true);
			ar.setData(deviceInOut);
		} catch (Exception e) {
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/in_device")
	public void inDevice(@RequestParam String tdCode, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) {
		ActionResult ar = new ActionResult();
		User user = (User) session.getAttribute(Constants.SESSION_ACCOUNT);
		try {
			Device device = deviceService.findByTdCode(tdCode);
			if(device != null){
				if(device.getCurrentStatus().getId() == 1){
					ar.setMsg("设备已入库");
				}
				else{
					DeviceStatus deviceStatus = new DeviceStatus();
					deviceStatus.setId(1);
					device.setCurrentStatus(deviceStatus);
					deviceService.update(device);
					DeviceInOut deviceInOut = new DeviceInOut();
					deviceInOut.setType(DeviceInOut.TYPE_IN);
					deviceInOut.setNo(device.getNo());
					deviceInOut.setTdCode(device.getTdCode());
					deviceInOut.setBrand(device.getBrand());
					deviceInOut.setDeviceType(device.getType().getName());
					deviceInOut.setManufacturers(device.getManufacturers());
					deviceInOut.setModel(device.getModel());
					deviceInOut.setOperator(user);
					deviceInOut.setTime(new Date());
					deviceInOutService.save(deviceInOut);
					ar.setSuccess(true);
					
					if(device.getWork() != null)
					{
						device.getWork().setOutCount(device.getWork().getOutCount() - 1);
						workService.update(device.getWork());
					}
				}
			}
			else{
				ar.setMsg("设备不存在");
			}
		} catch (Exception e) {
			ar.setMsg("入库失败");
			e.printStackTrace();
		}
		
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/delete")
	public void delete(Ids ids, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			deviceInOutService.delete(ids.getIds());
			ar.setSuccess(true);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/delete/{id}")
	public void delete(@PathVariable long id, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			deviceInOutService.delete(id);
			ar.setSuccess(true);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/all")
	public void all(HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		String[] limit = request.getParameterValues("limit");
		String[] pages = request.getParameterValues("page");
		int pagesize = 25;
		int page = 1;
		if(limit.length > 0){
			pagesize = Integer.valueOf(limit[0]);
		}
		if(pages.length > 0){
			page = Integer.valueOf(pages[0]);
		}
		try{
			long total = deviceInOutService.count();
			List<DeviceInOut> deviceInOuts = deviceInOutService.list(page, pagesize);
			ar.setSuccess(true);
			UITable table = new UITable();
			table.setTotal(total);
			table.setRows(deviceInOuts);
			ar.setData(table);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/search")
	public void search(UISearchDIO uiSearchDIO, HttpServletRequest request, HttpServletResponse response, HttpSession session){
		ActionResult ar = new ActionResult();
		try{
			session.setAttribute(Constants.SESSION_DIO_START_TIME, uiSearchDIO.getStartTime());
			session.setAttribute(Constants.SESSION_DIO_END_TIME, uiSearchDIO.getEndTime());
			String[] limit = request.getParameterValues("limit");
			String[] pages = request.getParameterValues("page");
			int pagesize = 25;
			int page = 1;
			if(limit.length > 0){
				pagesize = Integer.valueOf(limit[0]);
			}
			if(pages.length > 0){
				page = Integer.valueOf(pages[0]);
			}
			List<DeviceInOut> deviceInOuts = deviceInOutService.search(uiSearchDIO.getStartTime(), uiSearchDIO.getEndTime(), page, pagesize);
			ar.setSuccess(true);
			UITable table = new UITable();
			table.setTotal(deviceInOutService.countByCondition(uiSearchDIO.getStartTime(), uiSearchDIO.getEndTime()));
			table.setRows(deviceInOuts);
			ar.setData(table);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/export")
	public void export(HttpServletRequest request, HttpServletResponse response, HttpSession session){
		List<DeviceInOut> deviceInOuts = null;
		String startTime = (String) session.getAttribute(Constants.SESSION_DIO_START_TIME);
		String endTime = (String)session.getAttribute(Constants.SESSION_DIO_END_TIME);
		try {
//			String[] limit = request.getParameterValues("limit");
//			String[] pages = request.getParameterValues("page");
			int pagesize = Integer.MAX_VALUE;
			int page = 1;
//			if(limit.length > 0){
//				pagesize = Integer.valueOf(limit[0]);
//			}
//			if(pages.length > 0){
//				page = Integer.valueOf(pages[0]);
//			}
			deviceInOuts = deviceInOutService.search(startTime, endTime, page, pagesize);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(deviceInOuts != null && deviceInOuts.size() > 0){
			List<ReportDeviceInOut> rdios = new ArrayList<ReportDeviceInOut>();
			for(DeviceInOut dio : deviceInOuts){
				rdios.add(dio.toReportModel());
			}
			DeviceInOutUtil.export(response, rdios);
		}
	}
}
